<?php
/**
 * Контроллер административной панели, реализующий управление MVC-ресурсами
 * 
 * @category Phorm
 * @package Controller_Module_Control
 * @name /Phorm/Controller/Module/Control/Mvc.php
 * @copyright phorm.ru
 * @since 05.05.2012
 * @author vladimir@kachalov.net
 */

class Phorm_Controller_Module_Control_Mvc extends Phorm_Controller_Controller {
	
	/**
	 * Экземпляр класса управления mvc-ресурсами
	 * @var Phorm_Mvc
	 */
	public $Mvc;
	
	
	/**
	 * Предиспетчер
	 * 
	 * @return void
	 */
		
	public function preDispatch() {
		
		$this->Mvc = new Phorm_Mvc();
		
	}
	
	/**
	 * Редактирование MVC-ресурса
	 *
	 * @return void
	 */
	
	public function editAction() {
		
		if(isset($this->_params['resourceid']) && $ResourceInfo = $this->Mvc->getMvcInfoById($this->_params['resourceid'])) {
		
			$form = $this->Mvc->getMvcForm('edit');
			$form->setDefaults($ResourceInfo);
			
			if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
					
				$FormData = $form->getValues();
				$FormData['resourceid'] = $ResourceInfo['resourceid'];
					
				if($FormData = $this->Mvc->saveMvcInfo($FormData)) {
					$User = new Phorm_User();
					$this->_usersession->UserData = $User->getUserInfo(array('userid'=>$this->_usersession->UserData['userid']));
					$this->view->assign('Success',array($this->_translate->_('Изменения внесены')));
				} else {
					$this->composeTransactionErrorMessage($this->Mvc->_errors);
				}
				
			}
			
			$this->view->assign('Form',$form);
		
		} else {
			
			$this->view->assign('Errors',array($this->_translate->_('Материал с указанным Id не найден')));
			
		}
		
	}
	
	
	/**
	 * Пакетное управление доступом к MVC-ресурсам
	 *
	 * @return void
	 */
	
	public function aclAction() {
		
		$User = new Phorm_User();
		
		if($this->_request->isPost()) {
			
			$FormData = $this->_request->getPost();
			
			if(isset($FormData['acl']) && is_array($FormData['acl'])) {
			
				if($this->Mvc->updateRolesAcl($FormData['acl'])) {
					
					$this->_usersession->UserData = $User->getUserInfo(array('userid'=>$this->_usersession->UserData['userid']));
					$this->view->assign("Success",array($this->_translate->_('Изменения внесены')));
					
				} else {
					
					$this->composeTransactionErrorMessage($this->Mvc->_errors);
					
				}
			
			}
				
		}
		
		$this->view->assign('Resources',$this->Mvc->getModulesControllersActions());
		$this->view->assign('AclList',$this->Mvc->getAclList());
		$this->view->assign('Roles',$User->getRolesList());
		
	}
	
}